/*
 * Project Name: showtao
 * File Name: Account.java
 * Class Name: Account
 *
 * Copyright 2014 showtao.com
 *
 * Licensed under the showtao.com
 *
 * http://showtao.com
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.showtao.common.security.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.codehaus.jackson.annotate.JsonIgnore;

import com.showtao.common.security.enumeration.EUserStatus;
import com.showtao.front.domain.BaseDomain;
import com.showtao.front.domain.converter.EUserStatusConverter;

@SuppressWarnings("serial")
@Entity
@Table(name = "st_front_user")
public class User extends BaseDomain {

    @Id
    @Column(name = "user_name")
    private String userName;

    @Column
    private String password;
    
    @Column
    @Convert(converter = EUserStatusConverter.class)
    private EUserStatus status;

    @Column(name = "login_failure_ct")
    private int loginFailureCount;
    
    @JsonIgnore
    @ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
    @JoinTable(name = "st_user_role",
        joinColumns = {@JoinColumn(name = "user_name", referencedColumnName = "user_name")}, 
        inverseJoinColumns = {@JoinColumn(name = "role_name", referencedColumnName = "role_name")})
    @OrderBy("createdAt")
    private List<Role> roles;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public EUserStatus getStatus() {
        return status;
    }

    public void setStatus(EUserStatus status) {
        this.status = status;
    }

    public int getLoginFailureCount() {
        return loginFailureCount;
    }

    public void setLoginFailureCount(int loginFailureCount) {
        this.loginFailureCount = loginFailureCount;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (!this.getClass().isInstance(obj)) {
            return false;
        }
        return new EqualsBuilder().append(userName, ((User) obj).getUserName()).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(userName).toHashCode();
    }
}
